﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db8.Data;

namespace Db8.DataAccess
{
    public class UserManager
    {

        public static User GetUser(string Username)
        {
            using (Db8Context db = new Db8Context())
            {
                return db.Users.Include("UserRoles").Include("UserRoles.Role")
                    .Where(u => u.Username == Username)
                    .FirstOrDefault();
            }
        }

        public static void LogUser(long UserId, 
                                   string Username, 
                                   string Email, 
                                   DateTime LastUpdatedDt,
                                   string Name,
                                   string FirstName,
                                   string MiddleName,
                                   string LastName,
                                   string AccessToken)
        {
            using (Db8Context db = new Db8Context())
            {
                // find user, create if needed, update data
                User user = db.Users.Include("UserRoles").Include("UserRoles.Role")
                    .Where(u => u.UserId == UserId).FirstOrDefault();
                if (user == null)
                {
                    // create if needed
                    user = new User()
                    {
                        UserId = UserId,
                        Score = 1
                    };
                    db.Users.Add(user);
                }
                user.Username = Username;
                user.LastActivityDt = DateTime.UtcNow;
                user.LastUpdatedDt = LastUpdatedDt;
                user.Email = Email;
                user.Name = Name;
                user.FirstName = FirstName;
                user.MiddleName = MiddleName;
                user.LastName = LastName;
                user.AccessToken = AccessToken;

                Logger.LogInfomation(null, "LogUser", "Logged In: " + Username, string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", user.UserId, user.Username, user.LastUpdatedDt, user.Email, user.Name, user.FirstName, user.MiddleName, user.LastName));

                //db.SaveChanges();
                //if (db.Users.Count() == 1)
                //{
                //    Role sa = db.Roles.Where(r => r.Name == Db8Roles.SYSTEM_ADMINISTRATOR).FirstOrDefault();
                //    if (sa == null)
                //        db.Roles.Add(sa = new Role() { Name = Db8Roles.SYSTEM_ADMINISTRATOR });

                //    Role ca = db.Roles.Where(r => r.Name == Db8Roles.CONTENT_ADMINISTRATOR).FirstOrDefault();
                //    if (ca == null)
                //        db.Roles.Add(ca = new Role() { Name = Db8Roles.CONTENT_ADMINISTRATOR });

                //    if (!user.UserRoles.Any(ur => ur.Role.Name == sa.Name))
                //        user.UserRoles.Add(new UserRole() { User = user, Role = sa });
                //}

                db.SaveChanges();
            }
        }

    }
}
